class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int hash1[26] ={0};
        int hash2[26] ={0};
        int len_p = p.size();
        int len_s = s.size();
        int size_p=0;
        int size_s=0;
        for(int i=0;i<len_p;i++)
        {
            hash1[p[i]-'a']++;
            size_p++;
        }
        vector<int> ret;

        for(int left=0,right=0;right<len_s;right++)
        {
                hash2[s[right]-'a']++;
                size_s++;
                if(size_s>size_p)
                {
                    hash2[s[left]-'a']--;
                    size_s--;
                    left++;
                }
                bool flag=true;
                for(int i=0;i<26;i++)
                {
                    if(hash1[i]!=hash2[i])
                    {
                        flag = false;
                        break;
                    }
                }
                if(flag)
                    ret.push_back(left);
                    
        }
        return ret;
    }
};


///维护有效字符变量进行优化

class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
        int hash1[26] ={0};
        int hash2[26] ={0};
        int len_p = p.size();
        int len_s = s.size();
        for(int i=0;i<len_p;i++)
        {
            hash1[p[i]-'a']++;
        }
        vector<int> ret;

        for(int left=0,right=0,count=0;right<len_s;right++)
        {
            if(++hash2[s[right]-'a']<=hash1[s[right]-'a'])
            {
                count++;
            }
            if(right-left+1>len_p)
            {
                if(hash2[s[left]-'a']-- <=hash1[s[left]-'a'])
                    count--;
                left++;
            }
            if(count==len_p)
                ret.push_back(left);
                
        }
        return ret;
    }
};